/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb.config.boot;

import com.hyts.infodb.config.bean.ConfigDataBO;
import com.hyts.infodb.config.bean.ConfigDataVO;
import com.hyts.infodb.enums.ConfigDataEnum;
import com.hyts.infodb.config.service.ConfigDataService;
import com.hyts.infodb.context.SystemDataContainer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @project-name:infodb
 * @package-name:com.hyts.infodb.config.boot
 * @author:LiBo/Alex
 * @create-date:2020-02-21 11:59
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
@Configuration
@Slf4j
@EntityScan("com.hyts.infodb.config")
public class ConfigDataBootStrap implements CommandLineRunner {

    @Autowired
    ConfigDataService configDataService;

    @Override
    public void run(String... args) throws Exception {
        log.info("开始加载配置信息数据对象");
        ConfigDataBO param = new ConfigDataBO();
        ConfigDataEnum[] configDataEnums = ConfigDataEnum.values();
        for(ConfigDataEnum configDataEnum : configDataEnums){
            log.info("初始化:{} - {} ", configDataEnum.getDesc(),"开始");
            param.setConfigType(configDataEnum.getCode());
            List<ConfigDataVO> dataVOList = null;
            try {
                dataVOList = configDataService.findData(param);
            } catch (Exception e) {
                log.error("系统执行数据失败：{}", configDataEnum.getDesc());
                continue;
            }
            log.info("进行初始化数据信息：{} - {}", configDataEnum.getCode(),dataVOList);
            SystemDataContainer.DEFAULT_SYSTEM_DATA_CONTEXT.put(configDataEnum.getDesc(),dataVOList);
            log.info("初始化:{} - {} ", configDataEnum.getDesc(),"结束");
        }
        log.info("结束加载");
    }
}
